#!/bin/bash

# --- 配置 ---
SERVICE_NAME="bit-srun3000-chrome-driver-service.service"
SERVICE_FILE_SOURCE="./bit-srun3000-chrome-driver-service.service"
SERVICE_FILE_TARGET="/etc/systemd/system/${SERVICE_NAME}"

# 检查是否以 root 身份运行
if [ "$(id -u)" != "0" ]; then
   echo "此脚本必须以 root 身份运行" 1>&2
   exit 1
fi

# --- 函数 ---

install_service() {
    echo "正在安装服务..."
    
    if [ ! -f "${SERVICE_FILE_SOURCE}" ]; then
        echo "错误：找不到服务文件 ${SERVICE_FILE_SOURCE}"
        exit 1
    fi
    
    cp "${SERVICE_FILE_SOURCE}" "${SERVICE_FILE_TARGET}"
    
    systemctl daemon-reload
    
    systemctl enable "${SERVICE_NAME}"
    systemctl start "${SERVICE_NAME}"
    
    echo "服务 '${SERVICE_NAME}' 已成功安装并启动。"
    echo "使用 'systemctl status ${SERVICE_NAME}' 查看状态。"
}

uninstall_service() {
    echo "正在卸载服务..."
    
    systemctl stop "${SERVICE_NAME}"
    systemctl disable "${SERVICE_NAME}"
    
    if [ -f "${SERVICE_FILE_TARGET}" ]; then
        rm "${SERVICE_FILE_TARGET}"
    fi
    
    systemctl daemon-reload
    
    echo "服务 '${SERVICE_NAME}' 已成功卸载。"
}

# --- 主逻辑 ---
case "$1" in
    install)
        install_service
        ;;
    uninstall)
        uninstall_service
        ;;
    *)
        echo "用法: $0 {install|uninstall}"
        exit 1
        ;;
esac

exit 0
